import * as vscode from 'vscode';

export class TranslationHoverProvider implements vscode.HoverProvider {
  provideHover(
    document: vscode.TextDocument,
    position: vscode.Position,
    token: vscode.CancellationToken
  ): Thenable<vscode.Hover> {
    const wordRange = document.getWordRangeAtPosition(position);

    const word = wordRange ? document.getText(wordRange) : '';

    if (!word) {
      return Promise.resolve(new vscode.Hover('No word found.'));
    }

    return new Promise((resolve) => {
      let accumMsg = '';

      const askData = {
        "messages": [{ "role": "system", "content": `翻译：${word}` }],
        "model": "deepseek-chat",
        "options": {}
      };
      resolve(new vscode.Hover('测试悬停'))

     

      // fetch("https://api.deepseek.com/chat/completions", {
      //   method: 'POST',
      //   headers: {
      //     'Content-Type': 'application/json',
      //     'Authorization':'Bearer sk-de11e7d34ff2413c9228e583a937865b'
      //   },
      //   body: JSON.stringify(askData)
      // })
      // .then(response => response.text())
      // .then(data => {
      //   const arrayString = '[' + data.trim().replace(/}\s*{/g, '},{') + ']';
      //   let jsonArray;
      //   try {
      //     console.log('2222---->',2222);
      //     jsonArray = JSON.parse(arrayString);
      //     jsonArray.forEach((element: any) => {
      //       accumMsg += element.message.content;
      //     });
      //     let msg = 'AI翻译：' + accumMsg;
      //     resolve(new vscode.Hover(msg));
      //   } catch (e) {
      //     console.error("解析 JSON 失败1111", e);
      //     resolve(new vscode.Hover('Translation error: JSON parse failed.'));
      //   }
      // })
      // .catch(error => {
      //   console.error("请求失败", error);
      //   resolve(new vscode.Hover(`Translation error: ${error}`));
      // });

      // 如果超过一定时间未收到返回，则返回默认提示
      setTimeout(() => {
        resolve(new vscode.Hover('Translation timeout.'));
      }, 10000); // 超时时间设置为10秒
    });
  }
}